from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = "#2d2d2d" # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{(1998)计算：}$\lim\limits _{n\rightarrow \infty }\left( \dfrac{\sin \dfrac{\pi }{n}}{n+1}+\dfrac{\sin \dfrac{2\pi }{n}}{n+\dfrac{1}{2}}+\ldots +\dfrac{\sin \pi }{n+\dfrac{1}{n}}\right)$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        
        self.play(Write(text),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,20,0]) )
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=45,height=12
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([7.5, 20, 0])
        self.play(Create(rectangle))
        text1 = Tex(r"\text{思考流程固定}",color=YELLOW
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle,DOWN,buff=1)
        
        self.play(Write(text1))

        arrow = Arrow(start=[5,13,0],end=[5,8,0],color=RED
                       , stroke_width=20, tip_length=1)
      

        text2 = Tex(r"$\lim\limits _{n\rightarrow \infty }\displaystyle\sum ^{n}_{i=1}\dfrac{\sin \dfrac{1}{n}\pi }{n+\dfrac{1}{i}}$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,3,0])
        self.play(GrowArrow(arrow),ReplacementTransform(text1,text2))
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=9,height=4
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([10, 0, 0])
        self.play(Create(rectangle1))
        text3 = Tex(r"\text{i和n不同阶}",color=YELLOW
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle1,DOWN,buff=1)
        
        self.play(Write(text3))
        text4 = Tex(r"\text{夹逼定理}",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle1,DOWN,buff=1)
        self.play(ReplacementTransform(text3,text4),FadeOut(rectangle1))
        self.wait(0.2)
        text5 = Tex(r"\text{放缩}",color=YELLOW
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle1,DOWN,buff=1)
        self.play(ReplacementTransform(text4,text5))
        self.wait(0.2)
        text6 = Tex(r"\text{对i微观放缩}",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle1,DOWN,buff=1)
        self.play(ReplacementTransform(text5,text6))
        self.wait(0.2)

        text7 = Tex(r"\text{因为}$1\leq i\leq n$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,DOWN,buff=1)
        
        self.play(Write(text7))
        text8 = Tex(r"\text{那么}$\dfrac{1}{n}\leq \dfrac{1}{i}\leq 1$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text7,DOWN,buff=1)
        
        self.play(Write(text8))
        text9 = Tex(r"$\leq \lim\limits _{n\rightarrow \infty }\displaystyle\sum ^{n}_{i=1}\dfrac{\sin \dfrac{1}{n}\pi }{n+\dfrac{1}{n}}$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=1)
        text10 = Tex(r"$ \lim\limits _{n\rightarrow \infty }\displaystyle\sum ^{n}_{i=1}\dfrac{\sin \dfrac{1}{n}\pi }{n+1}\leq $",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,LEFT,buff=1)
        self.play(Write(text9),Write(text10))
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=17.5,height=10
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle2.move_to([-16.5, 2.5, 0])
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=17.5,height=10
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle3.move_to([26, 2.5, 0])
        
        self.play(Create(rectangle3),Create(rectangle2))
        self.play(
            rectangle2.animate.move_to([-11,0,0]).stretch_to_fit_width(6).stretch_to_fit_height(4.5),
            rectangle3.animate.move_to([31,0,0]).stretch_to_fit_width(7).stretch_to_fit_height(4.5)
        )

        text11 = Tex(r"$\leq \lim\limits _{n\rightarrow \infty }\dfrac{1}{n+\dfrac{1}{n}}\cdot \displaystyle\sum ^{n}_{i=1}\sin \dfrac{i}{n}\pi $",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=1)
        text12 = Tex(r"$ \lim\limits _{n\rightarrow \infty }\dfrac{1}{n+1}\cdot\displaystyle\sum ^{n}_{i=1}\sin \dfrac{i}{n}\pi \leq $",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,LEFT,buff=1)
        self.play(ReplacementTransform(text9,text11),ReplacementTransform(text10,text12),FadeOut(rectangle2,rectangle3))
        self.wait(0.2)


        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=2,height=7
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle2.move_to([-10, 3.2, 0])
        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=2,height=7
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle3.move_to([41, 3.7, 0])
        
        self.play(Create(rectangle3),Create(rectangle2))

        text13 = Tex(r"\text{凑定积分定义}",color=YELLOW
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle2,UP,buff=1)
        text14 = Tex(r"\text{凑定积分定义}",color=YELLOW
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle3,UP,buff=1)
        
        self.play(Write(text13),Write(text14))
        
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=8,height=7
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([-24, 3, 0])
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=8,height=8
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle5.move_to([27, 3.2, 0])
        text13 = Tex(r"$\dfrac{1}{n}$",color=GREEN
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle4,DOWN,buff=1)
        text14 = Tex(r"$\dfrac{1}{n}$",color=GREEN
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle5,DOWN,buff=1)
        self.play(Create(rectangle4),Create(rectangle5),Write(text13),Write(text14))


        arrow1 = Arrow(start=[-20,-2.5,0],end=[-20,-7.5,0],color=RED
                       , stroke_width=20, tip_length=1)
        arrow2 = Arrow(start=[30,-2.5,0],end=[30,-7.5,0],color=RED
                       , stroke_width=20, tip_length=1)
        text15 = Tex(r"$\int _{0}^{1}\sin \pi xdx$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-10,0])
        text16 = Tex(r"$\int _{0}^{1}\sin \pi xdx$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-10,0])
        
        
        self.play(GrowArrow(arrow1),GrowArrow(arrow2),Write(text15),Write(text16))

        text17 = Tex(r"$=\dfrac{2}{\pi }$",color=BLACK

                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-15,0])
        text18 = Tex(r"$=\dfrac{2}{\pi }$",color=BLACK
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,-15,0])
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.0,width=7,height=7
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([-20,-15,0])
        rectangle5=Rectangle(color=BLUE,fill_opacity=0.0,width=7,height=7
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle5.move_to([30,-15,0])
        self.play(Write(text17),Write(text18),Create(rectangle4),Create(rectangle5))

        text19 = Tex(r"$=\dfrac{2}{\pi }$",color=GREEN
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text,RIGHT,buff=1)
        self.play(ReplacementTransform(rectangle4,text19),ReplacementTransform(rectangle5,text19))
        self.wait(0.2)
        self.wait()